<?php
namespace Frontend\Modules\Video\Controllers;
use Common\Models\Videos\VideoCategory;
use Common\Models\Videos\National;
use Common\Models\Videos\Artist;
use Common\Models\Videos\Video;
use Common\Models\Videos\VideoGroup;
use Common\Models\Videos\VideoGroupLink;
use Common\Models\Videos\VideoCategoryLink;
use Common\Library\Common as LibraryCommon;
use Library\Constant\Constant;

class DetailController extends \FrontendController
{

    public function indexAction($id)
    {
        try {
        	//$this->view->users = Services::getService('users')->getLast();
            //Load banner 
            //Load js angularjs movies

            
            $this->assets
            ->addCss('vendor/frontend/css/video/detail/detail.css')
            ->addCss('vendor/frontend/css/video/category/category.css')
            ->addCss('vendor/frontend/css/home/home.css')
            ->addCss('vendor/prettyPhoto/css/prettyPhoto.css')
            ->addJs('vendor/frontend/js/video/detail/detail_angular.js')
            ->addJs('vendor/prettyPhoto/js/jquery.prettyPhoto.js')
            ->addJs('vendor/frontend/js/video/detail/detail.js');
           // ->addJs('vendor/frontend/js/home/movies.js');

            $video = Video::findFirstById($id);
            if(!$video)
            {
                $this->flash->error("Video was not found");
                return $this->response->redirect();

            }
            $video->setTotalView($video->getTotalView() + 1);
            //$video->save();

            //$group = VideoGroup::findFirstById($id);

            $videoCategoryLink = $video->videoCategoryLink->getFirst();
            $videoYouLikes = null;
            if($videoCategoryLink)
            {
                $videoCategoryId = $videoCategoryLink->getCategoryId();
                $videoYouLikes = $this->modelsManager->createBuilder()
                    ->from(array('video' => 'Common\Models\Videos\Video'))
                    ->groupBy('video.id')
                    ->leftJoin('Common\Models\Videos\VideoCategoryLink', 'video.id = category_link.video_id ','category_link')
                    ->where("category_link.category_id = $videoCategoryId")
                    ->orderBy('video.total_view')
                    ->getQuery()
                    ->execute();
            }


            $videoGroupLink = $video->videoGroupLink->getFirst();
            $videoGroups = null;
            if($videoGroupLink)
            {
                $groupVideoId = $videoGroupLink->getVideoGroupId();
                $videoGroups = $this->modelsManager->createBuilder()
                    ->from(array('video' => 'Common\Models\Videos\Video'))
                    ->groupBy('video.id')
                    ->leftJoin('Common\Models\Videos\VideoGroupLink', 'video.id = video_group_link.video_id ','video_group_link')
                    ->where("video_group_link.video_group_id = $groupVideoId")
                    ->orderBy('video_group_link.position')
                    ->getQuery()
                    ->execute();
            }
            $blockVideoGroup = $this->view->getRender('video' , 'block/video_group' , array('videos' => $videoGroups));

            $this->view->setVars(array(
                'video'         =>   $video,
                'videoYouLikes' => $videoYouLikes,
                'blockVideoGroup'  => $blockVideoGroup
            ));

            $this->view->render('video' , 'detail/index');
            
        } catch (\Exception $e) {
        	$this->flash->error($e->getMessage());
        }
    }

    public function watchFilmAction($slug)
    {
        try {
            //$this->view->users = Services::getService('users')->getLast();
            //Load banner 
            //Load js angularjs movies

            
            $this->assets
            ->addCss('vendor/frontend/css/video/detail/detail.css')
            ->addCss('vendor/frontend/css/video/category/category.css')
            ->addCss('vendor/frontend/css/home/home.css')
            ->addCss('vendor/prettyPhoto/css/prettyPhoto.css')
            ->addJs('vendor/frontend/js/video/detail/detail_angular.js')
            ->addJs('vendor/prettyPhoto/js/jquery.prettyPhoto.js')
            ->addJs('vendor/frontend/js/video/detail/detail.js');
           // ->addJs('vendor/frontend/js/home/movies.js');
            $keySlug = $this->constant->keyCache['video']['videoBySlug'] . '-' . $slug;
            $video = $this->modelsCache->get($keySlug);
            if(!$video){
                $video = Video::findFirstBySlug($slug);
                $this->modelsCache->save($keySlug , $video);
            }
            
            if(!$video)
            {
                $this->flash->error("Video was not found");
                return $this->response->redirect();

            }

            //$group = VideoGroup::findFirstById($id);
            $videoCategoryLink = $video->videoCategoryLink->getFirst();
            $videoYouLikes = null;
            
            if($videoCategoryLink)
            {
                $videoCategoryId = $videoCategoryLink->getCategoryId();
                $keyVideoLikeCategory = $this->constant->keyCache['video']['listVideoLikeCategoryId'] . '-' . $videoCategoryId;
                $videoYouLikes = $this->modelsCache->get($keyVideoLikeCategory);
                if(!$videoYouLikes){
                    $videoYouLikes = $this->modelsManager->createBuilder()
                    ->from(array('video' => 'Common\Models\Videos\Video'))
                    ->groupBy('video.id')
                    ->leftJoin('Common\Models\Videos\VideoCategoryLink', 'video.id = category_link.video_id ','category_link')
                    ->where("category_link.category_id = $videoCategoryId AND video.status = 'A'")
                    ->limit(50)
                    ->orderBy('video.total_view')
                    ->getQuery()
                    ->execute();
                    $this->modelsCache->save($keyVideoLikeCategory , $videoYouLikes);
                }
            }

            $blockVideoLikes = $this->view->getRender('video' , 'block/video' , array('videos' => $videoYouLikes , 'title' => 'Có thể bạn sẽ thích'));

            $videoGroupLink = $video->videoGroupLink->getFirst();
            $videoGroups = null;
            $titleGroup = '';
            if($videoGroupLink)
            {
                $groupVideoId = $videoGroupLink->getVideoGroupId();
                $titleGroup = $videoGroupLink->videoGroup->videoDescription->getName();
                $keyVideoInGroup = $this->constant->keyCache['video']['listVideoInGroup'] . '-' . $groupVideoId;
                $videoGroups = $this->modelsCache->get($keyVideoLikeCategory);
                if(!$videoGroups){
                    $videoGroups = $this->modelsManager->createBuilder()
                    ->from(array('video' => 'Common\Models\Videos\Video'))
                    ->groupBy('video.id')
                    ->leftJoin('Common\Models\Videos\VideoGroupLink', 'video.id = video_group_link.video_id ','video_group_link')
                    ->where("video_group_link.video_group_id = $groupVideoId AND video.status = 'A'")
                    ->orderBy('video_group_link.position')
                    ->getQuery()
                    ->execute();
                    $this->modelsCache->save($keyVideoInGroup , $videoGroups);
                }
               
            }
            $blockVideoGroup = $this->view->getRender('video' , 'block/video' , array('videos' => $videoGroups , 'title' => $titleGroup));

            $this->view->setVars(array(
                'video'         =>   $video,
                'blockVideoLikes' => $blockVideoLikes,
                'blockVideoGroup'  => $blockVideoGroup
            ));

            $this->view->render('video' , 'detail/index');

            if(!$this->cookies->has("haveWatchFilm" . $video->getId()) || $this->cookies->get("haveWatchFilm" . $video->getId()) == false){     
                $video->setTotalView($video->getTotalView() + 1);
                $video->save();
                $this->cookies->set("haveWatchFilm" . $video->getId() , true , time() + 86400);   
            } 
            
        } catch (\Exception $e) {
            $this->flash->error($e->getMessage());
        }
    }


    public function filmAction($slug)
    {
        try {
            
            $this->assets
            ->addCss('vendor/frontend/css/video/detail/detail.css')
            ->addCss('vendor/frontend/css/video/category/category.css')
            ->addCss('vendor/frontend/css/home/home.css')
            ->addCss('vendor/prettyPhoto/css/prettyPhoto.css')
            ->addJs('vendor/frontend/js/video/detail/detail_angular.js')
            ->addJs('vendor/prettyPhoto/js/jquery.prettyPhoto.js')
            ->addJs('vendor/frontend/js/video/detail/detail.js');

            $keySlug = $this->constant->keyCache['videoGroup']['videoGroupBySlug'] . '-' . $slug;
            $videoGroup = $this->modelsCache->get($keySlug);
            if(!$videoGroup)
            {
                $videoGroup = VideoGroup::findFirstBySlug($slug);   
                $this->modelsCache->save($keySlug , $videoGroup);
            }

            if(!$videoGroup)
            {
                $this->flash->error("Video was not found");
                return $this->response->redirect();

            }

            $id = $videoGroup->getId();
            $videoGroup->setTotalView($videoGroup->getTotalView() + 1);
            $videoGroup->save();

            $videoCategoryLink = $videoGroup->videoCategoryLink->getFirst();

            $videoGroupYouLikes = null;
            if($videoCategoryLink)
            {
                $videoCategoryId = $videoCategoryLink->getCategoryId();
                $keyVideoGroupLikeCategory = $this->constant->keyCache['videoGroup']['listVideoGroupLikeCategoryId'] . '-' . $videoCategoryId;
                $videoGroupYouLikes = $this->modelsCache->get($keyVideoGroupLikeCategory);
                if(!$videoGroupYouLikes){
                    $videoGroupYouLikes = $this->modelsManager->createBuilder()
                    ->from(array('videoGroup' => 'Common\Models\Videos\VideoGroup'))
                    ->groupBy('videoGroup.id')
                    ->leftJoin('Common\Models\Videos\VideoCategoryLink', 'videoGroup.id = categoryLink.video_group_id ','categoryLink')
                    ->where("categoryLink.category_id = $videoCategoryId AND videoGroup.id != $id AND videoGroup.status = 'A'")
                    ->limit(Constant::ITEM_PER_PAGE_20)
                    ->orderBy("videoGroup.total_view")
                    ->getQuery()
                    ->execute();

                    $this->modelsCache->save($keyVideoGroupLikeCategory , $videoGroupYouLikes);
                }
            }

            $blockVideoGroupYouLikes = $this->view->getRender('video' , 'block/video_group' , array('videos' => $videoGroupYouLikes , 'title' => 'Có thể bạn sẻ thích'));
           
            $videoGroupLink = $videoGroup->videoGroupLink->getFirst();

            $videoGroups = null;
            $titleGroup = '';
            if($videoGroupLink)
            {
                $groupVideoId = $videoGroupLink->getVideoGroupId();
                $titleGroup = $videoGroupLink->videoGroup->videoDescription->getName();
                $keyVideoInGroup = $this->constant->keyCache['video']['listVideoInGroup'] . '-' . $groupVideoId;
                $videoGroups = $this->modelsCache->get($keyVideoInGroup);
                if(!$videoGroups){
                    $videoGroups = $this->modelsManager->createBuilder()
                    ->from(array('video' => 'Common\Models\Videos\Video'))
                    ->groupBy('video.id')
                    ->leftJoin('Common\Models\Videos\VideoGroupLink', 'video.id = video_group_link.video_id ','video_group_link')
                    ->where("video_group_link.video_group_id = $groupVideoId AND video.status = 'A'")
                    ->orderBy('video_group_link.position')
                    ->getQuery()
                    ->execute();
                    $this->modelsCache->save($keyVideoInGroup , $videoGroups);
                }
            }
            $videoLast = null;
            if($videoGroups)
            {
                $videoLast = $videoGroups->getLast();
            }

            $blockVideoGroup = $this->view->getRender('video' , 'block/video_group' , array('videos' => $videoGroups , 'title' => $titleGroup));
           
            $blockMastheadFilm = $this->view->getRender('video' , 'block/masthead_film' , array('video' => $videoGroup , 'videoLast' => $videoLast , 'type' => 'video_group'));

            $this->view->setVars(array(
                'video'         =>   $videoGroup,
                'blockVideoGroupYouLikes' => $blockVideoGroupYouLikes,
                'blockVideoGroup'  => $blockVideoGroup,
                'blockMastheadFilm' => $blockMastheadFilm
            ));

            $this->view->render('video' , 'detail/film');
            
        } catch (\Exception $e) {
            $this->flash->error($e->getMessage());
        }
    }
}

